import time,os,asyncio
from util.Down import Download
from util.QQ import QQ
from util.pf import printf
def main():
    q = QQ()
    q.run()
    if not os.path.exists('album'):
        os.mkdir('album')
    if not os.path.exists(f'album/{q.QQ}'):
        os.mkdir(f'album/{q.QQ}')
    for i in q.a_info:
        if not os.path.exists(f"album/{q.QQ}/{i[0]}") and i[-1] != 0:
            os.mkdir(f"album/{q.QQ}/{i[0]}")
    for i in q.p_info:
        if i[-1] == '.mp4':
            q.videos.append(i)
            q.p_info.remove(i)
    q.p_info = list(filter(lambda i: "vwecam.gtimg.com" not in i[1], q.p_info))
    q.videos = list(filter(lambda i: "vwecam.gtimg.com" not in i[1], q.videos))
    printf(f"共{len(q.a_info)}个相册 {len(q.p_info)}张照片 {len(q.videos)}个视频")
    printf("开始下载照片")
    d = Download()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(d.main(q.p_info,q.QQ))
    printf(f"照片下载完成,共下载{d.count}张照片,失败{len(d.fail)}张照片")
    photo_fail = d.fail.copy()
    d.fail.clear()
    time.sleep(0.5)
    printf("开始下载视频")
    d.thread(q.videos,q.QQ)
    printf(f"视频下载完成,共下载{d.count}个视频,失败{len(d.fail)}个视频")
    video_fail = d.fail.copy()
    if len(d.fail) == 0:
        return
    printf("下载失败照片视频链接已保存至fail.txt")
    all_fail = photo_fail+video_fail
    with open("fail.txt","w",encoding="utf-8") as f:
        for i in all_fail:
            f.write(str(i[1]))
            f.write("\n")
if __name__ == '__main__':
    main()